﻿using System.IO;
using System.Web.UI.WebControls;
using PegasCompany.Data;
using System;
using System.Collections.Generic;
using System.Linq;
using PegasCompany.Helper;
using PegasCompany.Tools;
using PegasCompany.Tools.Text;

namespace PegasCompany.AdminPanel
{
    public partial class Slider : System.Web.UI.Page
    {
        private Slide CurrentSlide
        {
            get
            {
                object oCurrentSlide = ViewState["CurrentSlide"];
                return (oCurrentSlide != null) ? (Slide)oCurrentSlide : null;
            }
            set
            {
                ViewState["CurrentSlide"] = value;
            }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                if (!Request.QueryString.HasKeys())
                {
                    ShowSlideList();
                }
                else
                {
                    ShowSlideInfo();
                }
            }
        }

        private void ShowSlideList()
        {
            mvSlider.ActiveViewIndex = 0;

            List<Slide> slideList = Slide.Search();
            rptSlides.DataSource = slideList;
            rptSlides.DataBind();
        }

        private void ShowSlideInfo()
        {
            int iSlideId = PageHelper.GetIntValue(Request.QueryString["id"]);
            if (iSlideId == -1)
            {
                RedirectToDefaulPage();
            }
            Slide s = Slide.Get(iSlideId);
            if (s == null)
            {
                RedirectToDefaulPage();
                return;
            }

            CurrentSlide = s;

            txtSlideText.config.toolbar = PageHelper.EditorButtonSet;
            txtSlideText.config.width = "100%";
            mvSlider.ActiveViewIndex = 1;

            txtSlideTitle.Text = s.Title;
            txtSlideDescription.Text = s.Description;
            txtSlideText.Text = s.Text;
            txtSlidePopupTitle.Text = s.PopupTitle;
            txtSlidePopupText.Text = s.PopupText;
            txtSlideUrl.Text = s.Url;

            iSlidePhoto.ImageUrl = s.Photo;
            iSlideThumbnail.ImageUrl = s.Thumbnail;
        }

        private void RedirectToDefaulPage()
        {
            Response.Redirect("/AdminPanel/Slider.aspx");
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            if (CurrentSlide == null)
            {
                RedirectToDefaulPage();
            }
            else
            {
                CurrentSlide.Title = txtSlideTitle.Text.Trim();
                CurrentSlide.Description = txtSlideDescription.Text.Trim();
                CurrentSlide.Text = txtSlideText.Text.Trim();
                CurrentSlide.PopupTitle = txtSlidePopupTitle.Text.Trim();
                CurrentSlide.PopupText = txtSlidePopupText.Text.Trim();
                CurrentSlide.Url = txtSlideUrl.Text.Trim();

                if (!String.IsNullOrEmpty(fuSlidePhoto.FileName))
                {
                    CurrentSlide.Photo = fuSlidePhoto.FileName;
                    SavePhoto(CurrentSlide.SlideId, false);
                }

                if (!String.IsNullOrEmpty(fuSlideThumbnail.FileName))
                {
                    CurrentSlide.Thumbnail = fuSlideThumbnail.FileName;
                    SavePhoto(CurrentSlide.SlideId, true);
                }

                CurrentSlide.Update();
                ShowSlideInfo();
            }
        }

        private void SavePhoto(int? iSlideId, bool bIsThumbnail)
        {
            FileUpload fu = bIsThumbnail ? fuSlideThumbnail : fuSlidePhoto;

            if (String.IsNullOrEmpty(fu.FileName))
                return;

            if (bIsThumbnail && (!fu.HasFile || !FileValidate(fu.PostedFile.ContentType)))
                return;

            string sFullPath = Path.Combine(Server.MapPath(bIsThumbnail ? ConfigReader.SlideThumbnailPath : ConfigReader.SlidePhotoPath), fu.FileName);

            FileStream fsWriteAttachmentStream = new FileStream(sFullPath, FileMode.Create, FileAccess.ReadWrite);
            using (BinaryWriter binWriter = new BinaryWriter(fsWriteAttachmentStream))
            {
                binWriter.Write(FileHelper.ReadFileContent(fu.PostedFile.InputStream));
                binWriter.Close();
            }

            if (!File.Exists(sFullPath))
                return;

            Slide s = Slide.Get(iSlideId);

            if (bIsThumbnail)
            {
                s.Thumbnail = fu.FileName;
            }
            else
            {
                s.Photo = fu.FileName;
            }
            s.Update();

            //FileAttach emailTemplateAttach = new FileAttach
            //{
            //    SiteContentId = iSiteContentId,
            //    DocumentName = fuAttachFile.FileName,
            //    DocumentType = fuAttachFile.PostedFile.ContentType,
            //    DocumentSize = fuAttachFile.PostedFile.ContentLength
            //};

            //emailTemplateAttach.Save();
            //if (!emailTemplateAttach.AttachId.HasValue)
            //    return;

            //FillFileAttachList(iSiteContentId);
        }

        //private void FillFileAttachList(int siteContentId)
        //{
        //    btnSaveAttach.Visible = true;

        //    List<FileAttach> listFileAttach = FileAttach.ListBySiteContentId(siteContentId);
        //    if (listFileAttach != null)
        //    {
        //        rptContentAttach.DataSource = listFileAttach;
        //        rptContentAttach.DataBind();
        //    }
        //}


        protected bool FileValidate(string sContentType)
        {
            string[] sGrantedTypes = TextHelper.AllowedFileTypes;
            return Array.IndexOf(sGrantedTypes, sContentType) != -1;
        }

    }
}